import { takeEvery } from "redux-saga/effects";
import { NATIVE_CLASS } from "@common/constants";
import { callNativeFunc } from "@common/saga-helper";
import {
  SAGA_TRANSFER_IN_LOAD_OPS_FORM_DATA,
  SAGA_TRANSFER_IN_FINISH_OPS,
  SAGA_TRANSFER_IN_STASH_OPS_DATA,
  SAGA_TRANSFER_IN_LOAD_ITEM_DETAILS,
  SAGA_TRANSFER_IN_LOAD_BARCODE_DETAILS,
  SAGA_TRANSFER_IN_DEL_BARCODE,
  SAGA_TRANSFER_IN_PUSH_2_SERVER
} from "./saga-types";
import {
  SET_TRANSFER_IN_OPS_FORM_DATA,
  UPDATE_TRANSFER_IN_OPS_FORM_DATA,
  SET_TRANSFER_IN_ITEM_DETAILS,
  SET_TRANSFER_IN_BARCODE_DETAILS,
  DEL_TRANSFER_IN_BARCODE
} from "./action-types";

function* loadTransferInOpsFormDataSaga() {
  yield takeEvery(SAGA_TRANSFER_IN_LOAD_OPS_FORM_DATA, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.TransferInOpsHandler,
            action: "loadOpsFormData",
            needNonce: false,
            params: action.payload,
          },
          respActions: [SET_TRANSFER_IN_OPS_FORM_DATA]
        },
      ],
    });
  });
}

function* finishTransferInOpsSaga() {
  yield takeEvery(SAGA_TRANSFER_IN_FINISH_OPS, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.TransferInOpsHandler,
            action: "finish",
            needNonce: false,
            params: action.payload,
          },
          callback: action.payload.callback,
        },
      ],
    });
  });
}

function* stashTransferInOpsDataSaga() {
  yield takeEvery(SAGA_TRANSFER_IN_STASH_OPS_DATA, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.TransferInOpsHandler,
            action: "create",
            needNonce: false,
            params: action.payload,
          },
          respActions: [UPDATE_TRANSFER_IN_OPS_FORM_DATA],
          callback: action.payload.callback,
        },
      ],
    });
  });
}

function* loadTransferInItemDetailsSaga() {
  yield takeEvery(SAGA_TRANSFER_IN_LOAD_ITEM_DETAILS, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.TransferInDetailsHandler,
            action: "loadItemDetails",
            needNonce: false,
            params: action.payload,
          },
          respActions: [SET_TRANSFER_IN_ITEM_DETAILS],
        },
      ],
    });
  });
}

function* loadTransferInBarcodeDetailsSaga() {
  yield takeEvery(SAGA_TRANSFER_IN_LOAD_BARCODE_DETAILS, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.TransferInDetailsHandler,
            action: "loadBarcodeDetails",
            needNonce: false,
            params: action.payload,
          },
          respActions: [SET_TRANSFER_IN_BARCODE_DETAILS],
        },
      ],
    });
  });
}

function* delTransferInBarcodeSaga() {
  yield takeEvery(SAGA_TRANSFER_IN_DEL_BARCODE, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.TransferInDetailsHandler,
            action: "delBarcode",
            needNonce: false,
            params: action.payload,
          },
          respActions: [DEL_TRANSFER_IN_BARCODE],
          callback: action.payload.callback
        },
      ],
    });
  });
}

function* pushTransferIn2ServerSaga() {
  yield takeEvery(SAGA_TRANSFER_IN_PUSH_2_SERVER, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.TransferInHandler,
            action: "push2Server",
            needNonce: true,
            params: action.payload,
          },
          callback: action.payload.callback
        },
      ],
    });
  });
}

export default [
  loadTransferInOpsFormDataSaga(),
  finishTransferInOpsSaga(),
  stashTransferInOpsDataSaga(),
  loadTransferInItemDetailsSaga(),
  loadTransferInBarcodeDetailsSaga(),
  delTransferInBarcodeSaga(),
  pushTransferIn2ServerSaga()
];